
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {
    public $page_name = 'article';
    public function __construct(){
        parent::__construct();
        if (!$this->session->userdata('is_login')){
           redirect('/admin/auth/login');
        }
	}

	public function index($page=1){
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Article_model'); 
        $total = $this->Article_model->count_all();
        $page_size=4;     
        $data = array(
            'articles' => $this->Article_model->get_by_page($page,$page_size)
        );

        //$config['use_page_numbers'] = TRUE;
        $this->load->library('pagination');
        $config['base_url'] = site_url('/admin/article/index/');//分页
        $config['attributes'] = array('class' => 'myclass');//如果你想为分页类生成的每个链接添加额外的属性，你可以通过键值对设置 "attributes" 参数:
        $config['total_rows'] = $total;
        $config['per_page'] = $page_size;
        $this->pagination->initialize($config);

        $this->load->view('admin/article_index',$data);
	}

    public function add(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('title', '标题', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');
        $this->form_validation->set_rules('desc', '简介', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $article = new stdclass();
        $article ->catalog_id = '';
        $article ->title = '';
        $article ->img = '';
        $article ->desc = '';
        $article ->content = '';
        if ($this->form_validation->run() == FALSE){
            $this->load->model('Catalog_model');
            $data = array(
                    "catalogs" => $this->Catalog_model->get_entries(),
                    "title" => '添加',
                    "article" => $article
                    );
            $this->load->view('admin/article_form',$data);
        }
        else {
            $this->session->set_userdata('is_login', true);//添加
            $data = $this->input->post();
            $this->load->model('Article_model');
            $this->Article_model->add_one($data);
            redirect('/admin/article/index');
        }
    }

    public function edit($id){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('title', '标题', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');
        $this->form_validation->set_rules('desc', '简介', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $this->load->model('Article_model');
        if ($this->form_validation->run() == FALSE){
            $this->load->model('Catalog_model');
            $data = array(
                    "catalogs" => $this->Catalog_model->get_entries(),
                    "title" => '修改',
                    "article" => $this->Article_model->get_one($id)
                    );
            $this->load->view('admin/article_form',$data);
        }
        else {
            $this->session->set_userdata('is_login', true);
            $data = $this->input->post();
            $this->Article_model->edit_one($id,$data);
            redirect('/admin/article/index');
        }
    }

	public function del($id){
            $this->load->model('Article_model');
            $this->Article_model->del_one($id);
            redirect('/admin/article/index');
    }

    public function recommend($id,$recommend){
            if ($recommend==0) {
                $recommend=1;
            }else{
                $recommend=0;
            }
            $this->load->model('Article_model');
            $this->Article_model->recommend_one($id,$recommend);
            redirect('/admin/article/index');
    }
    
}
